<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Web3 Demo</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <script src="https://static.medishares.net/js/jquery-3.2.1.min.js"></script>
</head>
<body>
<br/>
<h1>测试网</h1>
<button onclick="SendETHTransaction()">SendETHTransaction</button>
<button onclick="SendERC20Transaction()">SendERC20Transaction</button>
<button onclick="PersonalSign()">PersonalSign</button>
<button onclick="EthSign()">EthSign</button>
<button onclick="CreateContract()">CreateContract</button>
<button onclick="Transfer()">Transfer</button>
<button onclick="FromWei()">FromWei</button>
<br/>
<h1>主网</h1>
<button onclick="MainRequestAccounts()">MainRequestAccounts</button>
<button onclick="GetBalance()">GetBalance</button>
<button onclick="MainSendETHTransaction()">MainSendETHTransaction</button>
<button onclick="MainSendERC20Transaction()">MainSendERC20Transaction</button>
<button onclick="MainSendERC20NoDecimalTransaction()">MainSendERC20NoDecimalTransaction</button>
<button onclick="MainPersonalSign()">MainPersonalSign</button>
<button onclick="MainEthSign()">MainEthSign</button>
<button onclick="MainCreateContract()">MainCreateContract</button>
<button onclick="MainTransfer()">MainTransfer</button>
<script type="text/javascript">
    var personal;
    window.onload=function(){
        // var Personal = require('web3-eth-personal');
        // 在以太坊兼容浏览器中，"Personal.providers.givenProvider"将自动被设置
        // 0.20.7
        if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            alert("web3 undefined");
            // $('#app_loading').hide();
            // alert(jQuery.i18n.prop('lrn_error_alert'));
            // mathWallet.closePage();
        }

        // creation of contract object
        // var LRNAirdropContract = web3.eth.contract(window.lrn_abi);
        // var LRCContract = web3.eth.contract(window.lrc_abi);
        // // initiate contract for an address
        // var myContractInstance = LRNAirdropContract.at('0xbf78B6E180ba2d1404c92Fc546cbc9233f616C42');
        // var lrcContractInstance = LRCContract.at('0xEF68e7C694F40c8202821eDF525dE3782458639f');

        web3.eth.getAccounts(function(error, accounts){
            console.log("in getAccounts:" + accounts);
            alert(accounts);
        });
    };

    function SendETHTransaction() {
        // web3.eth.signPersonalMessage;
        // web3.eth.sign("dssaddsa", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe");
        // 0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb
        // 0xdc5c83fbac54f0b10db9af9be5c3bf620226155e
        web3.eth.sendTransaction({
            from: "0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb",
            gasPrice: "20000000000",
            gas: "21000",
            to: '0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb',
            value: "100000000000000",
            data: ""
        }, function (error, result) {
            console.log("eth transfer error:" + JSON.stringify(error));
            console.log("eth transfer error:" + error);
            console.log("eth transfer result:" + JSON.stringify(result));
        });
    };

    function SendERC20Transaction() {
        var mtkAbi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];
        // var mtkContract = web3.eth.contract(mtkAbi).at("0x084d2D13d6AA6Dfb7F47Dc3c2376839025E022Ee");
        // mtkContract.transfer('0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb', 10000000000000000, function (error, result) {
        //     console.log("erc20 transfer error:" + JSON.stringify(error));
        //     console.log("erc20 transfer error:" + error);
        //     console.log("erc20 transfer result:" + JSON.stringify(result));
        // });

        var mtkContract = web3.eth.contract(mtkAbi).at("0x3c0bad69bb30a93f449dc1398ad02c0d5c0724ab");

        mtkContract.balanceOf("0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb", function (error, result) {
            alert(result);
        });
    };
    var contractIns;
    function CreateContract() {
        var mtkAbi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];
        contractIns = web3.eth.contract(mtkAbi).at("0x084d2D13d6AA6Dfb7F47Dc3c2376839025E022Ee");
    }

    function Transfer() {
        contractIns.transfer('0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb', 1, function (error, result) {
            alert(JSON.stringify(result));
        });
    }

    function FromWei() {
        alert(web3.fromWei('21000000000000', 'finney'));
    }

    function PersonalSign() {
        // web3.eth.personal.sign("dssaddsa", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe");
        web3.personal.sign("Hello world", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", function(error, result) {
            console.log("personal sign error:" + JSON.stringify(error));
            console.log("personal sign error:" + error);
            console.log("personal sign result:" + result);
        });
    };

    function EthSign() {
        web3.eth.sign("0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "dsakjd", function (error, result) {

        });
    }

    // =================主网
    function MainSendETHTransaction() {
        // web3.eth.signPersonalMessage;
        // web3.eth.sign("dssaddsa", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe");
        // 0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb
        // 0xdc5c83fbac54f0b10db9af9be5c3bf620226155e
        web3.eth.sendTransaction({
            from: "0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb",
            gasPrice: "20000000000",
            gas: "21000",
            to: '0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb',
            value: "100000000000000",
            data: ""
        }, function (error, result) {
            console.log("eth transfer error:" + JSON.stringify(error));
            console.log("eth transfer error:" + error);
            console.log("eth transfer result:" + JSON.stringify(result));
        });
    };

    function MainSendERC20Transaction() {
        var mtkAbi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];
        var mtkContract = web3.eth.contract(mtkAbi).at("0x3c0bad69bb30a93f449dc1398ad02c0d5c0724ab");
        mtkContract.transfer('0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb', 100000000, function (error, result) {
            console.log("erc20 transfer error:" + JSON.stringify(error));
            console.log("erc20 transfer error:" + error);
            console.log("erc20 transfer result:" + JSON.stringify(result));
        });
    };

    function MainSendERC20NoDecimalTransaction() {
        var mtkAbi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];
        var mtkContract = web3.eth.contract(mtkAbi).at("0x38c6a68304cdefb9bec48bbfaaba5c5b47818bb2");
        mtkContract.transfer('0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb', 10000000000000000, function (error, result) {
            console.log("erc20 transfer error:" + JSON.stringify(error));
            console.log("erc20 transfer error:" + error);
            console.log("erc20 transfer result:" + JSON.stringify(result));
        });
    };

    function MainCreateContract() {
        var mtkAbi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];
        contractIns = web3.eth.contract(mtkAbi).at("0x1914caa9f41468ccdfaa8fc72294a4a51a3bb379");
    }

    function MainTransfer() {
        contractIns.transfer('0x3BBf0A387a73022A9ebC2491DC4fA8A465C8aAbb', 1, function (error, result) {
            alert(JSON.stringify(result));
        });
    }

    function MainPersonalSign() {
        // web3.eth.personal.sign("dssaddsa", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe");
        web3.personal.sign("Hello world", "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", function(error, result) {
            console.log("personal sign error:" + JSON.stringify(error));
            console.log("personal sign error:" + error);
            console.log("personal sign result:" + result);
        });
    };

    function MainEthSign() {
        web3.eth.sign("0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "dsakjd", function (error, result) {

        });
    }

    function MainRequestAccounts() {
        web3.eth.getAccounts(function(error, accounts){
            console.log("in getAccounts:" + accounts);
        });
        console.log("web3.eth.accounts:" + web3.eth.accounts);
        window.ethereum.enable();

        console.log("web3.currentProvider:" + JSON.stringify(web3.currentProvider))
    }

    function GetBalance() {
        web3.eth.getBalance("0xC29349361e65f23F5d917A1B5922C03727deDB64", function (error, results) {
            console.log("in results:" + results);
            // alert("results:" + results);
        });
    }

</script>
</body>
</html>
